﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Issues.IssueQuestTasks.DefeatPartyQuestTask
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.MapEvents;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Issues.IssueQuestTasks
{
  public class DefeatPartyQuestTask : QuestTaskBase
  {
    private DefeatPartyQuestTask.PartyConditionDelegateType _partyConditionDelegate;
    private DefeatPartyQuestTask.OnPartyDefeatedDelegateType _onPartyDefeatedDelegate;
    [SaveableField(40)]
    private readonly List<PartyBase> _partiesToDefeat;
    [SaveableField(41)]
    private int _targetNumParties;
    [SaveableField(42)]
    private int _deferatedNumParties;
    [SaveableField(43)]
    private TextObject _partyType;
    [SaveableField(44)]
    private bool _finishOnFail;

    internal static void AutoGeneratedStaticCollectObjectsDefeatPartyQuestTask(
      object o,
      List<object> collectedObjects)
    {
      ((QuestTaskBase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this._partiesToDefeat);
      collectedObjects.Add((object) this._partyType);
    }

    internal static object AutoGeneratedGetMemberValue_partiesToDefeat(object o)
    {
      return (object) ((DefeatPartyQuestTask) o)._partiesToDefeat;
    }

    internal static object AutoGeneratedGetMemberValue_targetNumParties(object o)
    {
      return (object) ((DefeatPartyQuestTask) o)._targetNumParties;
    }

    internal static object AutoGeneratedGetMemberValue_deferatedNumParties(object o)
    {
      return (object) ((DefeatPartyQuestTask) o)._deferatedNumParties;
    }

    internal static object AutoGeneratedGetMemberValue_partyType(object o)
    {
      return (object) ((DefeatPartyQuestTask) o)._partyType;
    }

    internal static object AutoGeneratedGetMemberValue_finishOnFail(object o)
    {
      return (object) ((DefeatPartyQuestTask) o)._finishOnFail;
    }

    public DefeatPartyQuestTask(
      List<PartyBase> partiesToDefeat,
      Action onSucceededAction,
      Action onFailedAction,
      Action onCanceledAction,
      DialogFlow dialogFlow = null,
      bool finishOnFail = false)
      : base(dialogFlow, onSucceededAction, onFailedAction, onCanceledAction)
    {
      this._partiesToDefeat = new List<PartyBase>();
      foreach (PartyBase partyBase in partiesToDefeat)
        this._partiesToDefeat.Add(partyBase);
      this._targetNumParties = partiesToDefeat.Count;
      this._partyType = partiesToDefeat[0].Name;
      this._finishOnFail = finishOnFail;
    }

    public DefeatPartyQuestTask(
      DefeatPartyQuestTask.PartyConditionDelegateType partyCondition,
      DefeatPartyQuestTask.OnPartyDefeatedDelegateType onPartyDefeated,
      int targetNumParties,
      Action onSucceededAction,
      Action onFailedAction,
      Action onCanceledAction,
      DialogFlow dialogFlow = null)
      : base(dialogFlow, onSucceededAction, onFailedAction, onCanceledAction)
    {
      this._partyConditionDelegate = partyCondition;
      this._targetNumParties = targetNumParties;
      this._onPartyDefeatedDelegate = onPartyDefeated;
    }

    public void OnMapEventEnded(MapEvent mapEvent)
    {
      if (!mapEvent.IsPlayerMapEvent)
        return;
      if (PartyBase.MainParty.Side == mapEvent.WinningSide)
      {
        foreach (PartyBase involvedParty in mapEvent.InvolvedParties)
        {
          if (involvedParty.Side == mapEvent.DefeatedSide)
          {
            if (this._partyConditionDelegate == null && this._partiesToDefeat.Contains(involvedParty))
            {
              this._partiesToDefeat.Remove(involvedParty);
              ++this._deferatedNumParties;
            }
            else if (this._partyConditionDelegate != null && this._partyConditionDelegate(involvedParty))
              ++this._deferatedNumParties;
            if (this._onPartyDefeatedDelegate != null)
              this._onPartyDefeatedDelegate(involvedParty);
          }
        }
        if (this._deferatedNumParties < this._targetNumParties)
          return;
        this.Finish(QuestTaskBase.FinishStates.Success);
      }
      else
      {
        if (PartyBase.MainParty.Side != mapEvent.DefeatedSide || !this._finishOnFail)
          return;
        foreach (PartyBase involvedParty in mapEvent.InvolvedParties)
        {
          if (this._partyConditionDelegate == null && this._partiesToDefeat.Contains(involvedParty))
          {
            this.Finish(QuestTaskBase.FinishStates.Fail);
            break;
          }
        }
      }
    }

    protected override void OnFinished()
    {
      if (this._partiesToDefeat == null)
        return;
      this._partiesToDefeat.Clear();
    }

    public override void SetReferences()
    {
      CampaignEvents.MapEventEnded.AddNonSerializedListener((object) this, new Action<MapEvent>(this.OnMapEventEnded));
    }

    public delegate bool PartyConditionDelegateType(PartyBase defeatedParty);

    public delegate void OnPartyDefeatedDelegateType(PartyBase party);
  }
}
